import Observer from './observer';

module.exports = class Store {
  constructor(confs = {}) {
    this.state = {};
    Object.keys(confs).forEach(key => {
      Object.defineProperty(this.state, key, {
        enumerable: true, // 允许被枚举
        get: () => {
          return this.get(key);
        },
        set: (value) => {
          return this.set(key, value)
        },
      });
    });
    Object.defineProperty(this, '_confs', {
      enumerable: false,
      value: JSON.parse(JSON.stringify(confs))
    });
  }

  get(key) {
    return this._confs[key];
  }

  set(key, value) {
    Observer.subscriber(key, value, this._confs[key]);
    this._confs[key] = value;
  }
}